package cn.gson.boot.controller;

import cn.gson.boot.model.dao.ContactsDao;
import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.ContactsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/contact")
public class ContactsController {
    @Autowired
    ContactsService contactsService;

    @Autowired
    ContactsDao contactsDao;

    //联系人分页查询 和 所有查询
    @RequestMapping("/contact-list")
    @ResponseBody
    public Map<String,Object> contactList(String cName, String cSex, Integer pageNum, Integer pageSize, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum, pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        int empId  = 0;
        System.err.println("IDIDIDIDI:"+us.getEmpId());
        if(jueNameList.indexOf("boss")>-1){
            empId = 0;
        }else{
            empId = us.getEmpId();
        }

        List<Contacts> contactsList = contactsService.allContacts(cName,cSex,empId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",contactsList);
        return map;
    }

    //联系人详情查询
    @RequestMapping("/contact-xqlist")
    @ResponseBody
    public Contacts xqContacts(Integer cId){
        Contacts contactsList1 = contactsService.xqContacts(cId);
        return contactsList1;
    }

    //新增联系人
    @RequestMapping("/add-contact")
    @ResponseBody
    public String  addContacts(@RequestBody Contacts contacts){
        String addcontacts  = contactsService.addContacts(contacts);
        return addcontacts;
    }

    @RequestMapping("/delete-contact")
    @ResponseBody
    public void deleteContacts(int id){
        contactsService.deleteContacts(id);
    }
}
